package com.eastfair.notice.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.notice.dto.ImRecordPageQuery;
import com.eastfair.notice.dto.ImRecordSaveDTO;
import com.eastfair.notice.dto.ImRecordUpdateDTO;
import com.eastfair.notice.entity.ImRecord;
import com.eastfair.notice.service.ImDialogService;
import com.eastfair.notice.service.ImRecordService;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * Im聊天对话内容表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/imRecord")
@Api(value = "ImRecord", tags = "Im聊天对话内容表")
public class ImRecordController extends SuperController<ImRecordService, Long, ImRecord, ImRecordPageQuery, ImRecordSaveDTO, ImRecordUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ImDialogService imDialogService;

    @Override
    public void handlerResult(IPage<ImRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ImRecord> imRecordList = list.stream().map((map) -> {
            ImRecord imRecord = ImRecord.builder().build();
            //TODO 请在这里完成转换
            return imRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(imRecordList));
    }

    /**
     * 保存聊天记录接口
     * @param record 聊天记录entity
     * @return true/false
     */
    @PostMapping("/saveRecord")
    @ApiOperation(value = "保存聊天记录",notes = "保存聊天记录")
    public R<Boolean> saveRecord(@RequestBody @Validated ImRecord record) {
        //保存对话并返回对话唯一ID
        String uniqueCode = this.imDialogService.saveDialog(record.getProjectId(),record.getFromUserId(),record.getToUserId());
        record.setUniqueCode(uniqueCode);
        return success(baseService.save(record));
    }

    /**
     * 分页查询聊天列表
     * @param params
     * @return
     */
    @ApiOperation(value = "分页查询聊天列表",notes = "分页查询聊天列表")
    @Override
    public R<IPage<ImRecord>> page(@RequestBody PageParams<ImRecordPageQuery> params) {
        ImRecordPageQuery query = params.getModel();
        QueryWrapper<ImRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ImRecord::getProjectId,ContextUtil.getProjectId())
                .eq(ImRecord::getUniqueCode,query.getUniqueCode())
                //时间段查询
                .gt(query.getStartTime() != null,ImRecord::getCreateTime,query.getStartTime())
                .lt(query.getEndTime() != null,ImRecord::getCreateTime,query.getEndTime())
                //聊天内容模糊查询
                .like(!StrUtil.isEmpty(query.getContent()),ImRecord::getContent,query.getContent());
        return success(baseService.page(params.buildPage(),queryWrapper));
    }
}
